package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 加入商品到购物车
     * @param shoppingCartDTO
     */
    @Override
    public void insert(ShoppingCartDTO shoppingCartDTO) {
        //就是往表加数据，那就要创建ShoppingCart对象
        //1.要userId区分购物车 -> 转成shoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断当前商品是否再购物车中 -- 根据userId 动态查询，可能是套餐可能是菜品,根据userId 和 商品 只会查出唯一的一条或者没有
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByUserId(shoppingCart);

        //如果存在，那就是数量+1
        if(shoppingCartList != null && !shoppingCartList.isEmpty()){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1); //属性改了
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.updateNumberById(shoppingCart); //更新数据库
        }else {
            //如果不存在，插入数据，数量是1 -- 要分插入套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                //1.加的是菜品，那就要把口味也加上 -- dto存了
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //2.加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }



    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                                        .userId(BaseContext.getCurrentId())
                                        .build();
        shoppingCartMapper.selectByUserId(shoppingCart);
        return shoppingCartMapper.selectByUserId(shoppingCart);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void remove(ShoppingCartDTO shoppingCartDTO) {
        //传进来的是DTO,有可能删的是菜品，有可能删的是套餐，-- 动态删除
        //删的菜品如果数量大于1，那只需要数量-1, 更新数量，如果等于1，就要删除这条购物车数据(动态删除)
        //1.查询这条数据的数量
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByUserId(shoppingCart);
        shoppingCart = shoppingCartList.get(0);
        if(shoppingCart.getNumber() > 1){
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            shoppingCartMapper.delete(shoppingCart);
        }
    }

    @Override
    public void delete() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
